Skip to main content

C# : Interview questions (6-10)


 Questions :

  • What is the difference between stack and heap memory?
  • How do you declare a variable in C#?
  • Explain the use of the "var" keyword in C#.
  • What are the different access modifiers in C#?
  • How does C# support polymorphism?

Answers :

Difference between Stack and Heap Memory

Stack and heap are two types of memory used by computer programs, including those written in C#. Here's how they differ:

  1. Stack Memory:

    • Stack memory is used for storing local variables and function call information.
    • It operates in a Last In, First Out (LIFO) manner, where the last item added is the first to be removed.
    • Memory allocation and deallocation on the stack are fast because it involves adjusting the stack pointer.
    • Stack memory is limited in size and is typically smaller than heap memory.
    • Stack memory is automatically managed by the compiler or runtime environment.
  2. Heap Memory:

    • Heap memory is used for dynamic memory allocation, where memory is allocated and deallocated explicitly by the programmer.
    • It operates in a more flexible manner, allowing memory to be allocated and deallocated in any order.
    • Memory allocation and deallocation on the heap can be slower than on the stack because it involves searching for suitable free memory blocks.
    • Heap memory is typically larger than stack memory and can grow dynamically as needed.
    • Heap memory management is the responsibility of the programmer, who must manually allocate and deallocate memory using functions like new and delete (or in C#, using constructs like new and garbage collection).

Declaring a Variable in C#

In C#, you can declare a variable using the following syntax:

<data-type> <variable-name>;
Here, <data-type> refers to the type of data the variable will hold, such as int, float, string, etc., and <variable-name> is the name you give to the variable. For example
int age;
string name;
float salary;

These statements declare variables age, name, and salary of types int, string, and float, respectively.

Use of the "var" Keyword in C#

The "var" keyword in C# is used for implicit typing, allowing the compiler to infer the type of a variable based on the value assigned to it. Here's how it works

var variableName = value;
Instead of explicitly specifying the data type of the variable, you can use "var", and the compiler will determine the appropriate data type based on the value assigned to the variable. For example
var age = 25; // Compiler infers int type
var name = "John"; // Compiler infers string type
var salary = 50000.50f; // Compiler infers float type

The "var" keyword is especially useful when the type of the variable is obvious from the initialization expression, or when dealing with complex types like LINQ queries or anonymous types.

Different Access Modifiers in C#

C# provides several access modifiers to control the accessibility of classes, methods, properties, and other members within a program. The main access modifiers in C# are:

  1. public: The public access modifier allows members to be accessed from any other class or assembly.

  2. private: The private access modifier restricts the accessibility of members to the containing class only. They cannot be accessed from outside the class.

  3. protected: The protected access modifier allows members to be accessed within the containing class and its derived classes.

  4. internal: The internal access modifier restricts access to members to the current assembly. Members marked as internal can be accessed from any class within the same assembly but not from outside assemblies.

  5. protected internal: The protected internal access modifier combines the behavior of protected and internal. It allows members to be accessed within the same assembly or from derived classes, even if they are in different assemblies.

Support for Polymorphism in C#

C# supports polymorphism through various mechanisms, including inheritance, interfaces, and method overriding. Polymorphism allows objects of different types to be treated as objects of a common base type, simplifying code and enabling flexibility. Here's how C# supports polymorphism:

  1. Inheritance: C# allows classes to inherit from other classes, inheriting their members and behaviors. This enables polymorphic behavior, where objects of derived classes can be treated as objects of their base class.

  2. Interfaces: Interfaces define a contract that classes can implement. They enable polymorphic behavior by allowing objects of different classes to be treated uniformly based on their shared interface.

  3. Method Overriding: C# allows derived classes to override base class methods, providing their own implementation. This enables polymorphic behavior, where the same method call can exhibit different behavior depending on the actual type of the object.

By leveraging these features, C# enables developers to write code that is flexible, extensible, and easier to maintain, promoting the principles of polymorphism in object-oriented programming.

C# : Interview questions (11-15)

Comments

Popular posts from this blog

Implementing and Integrating RabbitMQ in .NET Core Application: Shopping Cart and Order API

RabbitMQ is a robust message broker that enables communication between services in a decoupled, reliable manner. In this guide, we’ll implement RabbitMQ in a .NET Core application to connect two microservices: Shopping Cart API (Producer) and Order API (Consumer). 1. Prerequisites Install RabbitMQ locally or on a server. Default Management UI: http://localhost:15672 Default Credentials: guest/guest Install the RabbitMQ.Client package for .NET: dotnet add package RabbitMQ.Client 2. Architecture Overview Shopping Cart API (Producer): Sends a message when a user places an order. RabbitMQ : Acts as the broker to hold the message. Order API (Consumer): Receives the message and processes the order. 3. RabbitMQ Producer: Shopping Cart API Step 1: Install RabbitMQ.Client Ensure the RabbitMQ client library is installed: dotnet add package RabbitMQ.Client Step 2: Create the Producer Service Add a RabbitMQProducer class to send messages. RabbitMQProducer.cs : using RabbitMQ.Client; usin...

How Does My .NET Core Application Build Once and Run Everywhere?

One of the most powerful features of .NET Core is its cross-platform nature. Unlike the traditional .NET Framework, which was limited to Windows, .NET Core allows you to build your application once and run it on Windows , Linux , or macOS . This makes it an excellent choice for modern, scalable, and portable applications. In this blog, we’ll explore how .NET Core achieves this, the underlying architecture, and how you can leverage it to make your applications truly cross-platform. Key Features of .NET Core for Cross-Platform Development Platform Independence : .NET Core Runtime is available for multiple platforms (Windows, Linux, macOS). Applications can run seamlessly without platform-specific adjustments. Build Once, Run Anywhere : Compile your code once and deploy it on any OS with minimal effort. Self-Contained Deployment : .NET Core apps can include the runtime in the deployment package, making them independent of the host system's installed runtime. Standardized Libraries ...

.NET 10: Your Ultimate Guide to the Coolest New Features (with Real-World Goodies!)

 Hey .NET warriors! 🤓 Are you ready to explore the latest and greatest features that .NET 10 and C# 14 bring to the table? Whether you're a seasoned developer or just starting out, this guide will show you how .NET 10 makes your apps faster, safer, and more productive — with real-world examples to boot! So grab your coffee ☕️ and let’s dive into the awesome . 💪 1️⃣ JIT Compiler Superpowers — Lightning-Fast Apps .NET 10 is all about speed . The Just-In-Time (JIT) compiler has been turbocharged with: Stack Allocation for Small Arrays 🗂️ Think fewer heap allocations, less garbage collection, and blazing-fast performance . Better Code Layout 🔥 Hot code paths are now smarter, meaning faster method calls and fewer CPU cache misses. 💡 Why you care: Your APIs, desktop apps, and services now respond quicker — giving users a snappy experience . 2️⃣ Say Hello to C# 14 — More Power in Your Syntax .NET 10 ships with C# 14 , and it’s packed with developer goodies: Field-Bac...